<?php

/**
 * Admin Controller
 * ระบบ Admin หลักของ system
 */
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Admin system
 * ระบบ admin เพื่อจัดการระบบหลักของเว็บ
 * @package Adminsystem
 * @subpackage Controller
 * @category main
 * @author BeYourCyber
 * @version 1.0
 */
class Admin extends Admin_Controller {

    function __construct() {
        parent::Admin_Controller();
        $this->load->library('tank_auth');
        
    }

    /**
     * Index Admin
     * หน้าต้อนรับ Admin
     * @uses tank_auth
     */
    function index() {
        $cl = $this->__modulereference;
        $this->$cl->lang('admin_system');
        if ($this->tank_auth->is_logged_in()) {
            //check admin
            if ($this->tank_auth->is_admin()) {
                $this->$cl->view('admin_v');
            } else {
                header('Content-type: text/html; charset=utf-8');
                echo $this->lang->line('admin_system.not_permission');
            }
        } else {
            redirect('');
        }
    }

    function changepass() {
        
        $cl = $this->__modulereference;
        $this->$cl->lang('admin_system');
        $this->lang->load('tank_auth');
        $this->load->library(array('form_validation'));
        if ($this->tank_auth->is_logged_in()) {
            //check admin
            if ($this->tank_auth->is_admin()) {
                $this->form_validation->set_rules('old_password', 'Old Password', 'trim|required|xss_clean');
                $this->form_validation->set_rules('new_password', 'New Password', 'trim|required|xss_clean|min_length[' . $this->config->item('password_min_length', 'tank_auth') . ']|max_length[' . $this->config->item('password_max_length', 'tank_auth') . ']|alpha_dash');
                $this->form_validation->set_rules('confirm_new_password', 'Confirm new Password', 'trim|required|xss_clean|matches[new_password]');

                $data['errors'] = array();

                if ($this->form_validation->run()) {        // validation ok
                    if ($this->tank_auth->change_password(
                                    $this->form_validation->set_value('old_password'),
                                    $this->form_validation->set_value('new_password'))) { // success
                        $this->_show_message($this->lang->line('auth_message_password_changed'));
                        return;
                    } else {              // fail
                        $errors = $this->tank_auth->get_error_message();
                        foreach ($errors as $k => $v)
                            $data['errors'][$k] = $this->lang->line($v);
                    }
                }
//                $this->load->view('auth/change_password_form', $data);
                $this->$cl->view('change_pass_v',$data);
            } else {
                header('Content-type: text/html; charset=utf-8');
                echo $this->lang->line('admin_system.not_permission');
            }
        } else {
            redirect('');
        }
    }

    /**
     * ทดสอบการเรียก และแสดงผล multi language
     * @uses check_language.php view
     * @uses about_lang.php language
     */
    function check_language() {
        $thiscl = $this->__modulereference;
        $this->load->helper(array('language', 'url'));
        $this->$thiscl->lang("about");
        $this->$thiscl->view('check_language');
    }

    /**
     * ทดสอบ สถานะ login
     * โค้ดตัวอย่างเรียกและแสดงผลการ login
     */
    function check_login() {
        $thiscl = $this->__modulereference;
        $this->load->helper(array('language', 'url'));
        if (!$this->tank_auth->is_logged_in()) {
            echo 'not logged in';
        } else {
            echo 'logged in';
        }
    }

    /**
     * ทดสอบการเรียก asset
     * โดยใช้ ./file/
     * @uses test_file.php view
     * @uses files
     */
    function test_file() {
        $this->load->library('bee');
        $this->load->helper('url');
        $cl = $this->__modulereference;
        //use for test ./file/ only
        $this->$cl->view('test_file');
    }

    /**
     * ตัวอย่างโค้ด หน้า Admin
     * หน้าตัวอย่าง สำหรับส่วน admin
     * @uses admin.php view
     */
    function admin() {
        $this->load->library('bee');
        $this->load->helper('url');
        $this->load->library('adminlib');
        $this->load->library('cache');
        $cl = $this->__modulereference;

        $this->$cl->view('admin');
    }

    /**
     * Set theme
     * controller สำหรับการเปลี่ยน theme เมื่อเปลี่ยนแล้วจะ redirect กลับไปหน้าเดิม
     * @uses Bee::getTheme() Library
     */
    function set_theme($id) {
        $this->load->library('bee');
        $this->load->helper('cookie');
        $cookie = array(
            'name' => 'theme',
            'value' => $id,
            'expire' => '86500'
        );
        set_cookie($cookie);
        //echo $this->bee->getTheme();
        $destination = htmlspecialchars(getenv("HTTP_REFERER"), ENT_QUOTES, 'UTF-8');
        if (isset($destination{10})) {
            header("Location: " . $destination);
        }
    }

}
